﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using ARR.Entities;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for SpacesImporterView.xaml
    /// </summary>
    public partial class SpacesImporterView : UserControl
    {
        public SpacesImporterView()
        {
            InitializeComponent();
        }

        private void ResultsGV_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            if (e.Row is GridViewRow && !(e.Row is GridViewNewRow))
            {
                ImporterResult importResult = e.DataElement as ImporterResult;

                foreach (var cell in e.Row.Cells)
                {
                    if (cell.Column.Name == "SelectionColumn")
                    {
                        CheckBox check = cell.Content as CheckBox;
                        if (check != null)
                            check.Visibility = (importResult.Workaround.HasValue && importResult.Workaround == Workaround.CategoryMargin) ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
            }
        }

        private void ResultsGV_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            //SalesImporterViewModel vm = this.DataContext as SalesImporterViewModel;

            //if (vm != null)
            //{
            //    ImporterResult result = ResultsGV.SelectedItem as ImporterResult;

            //    if (result != null)
            //    {
            //        List<TempArticleSale> selectedSales = new List<TempArticleSale>();

            //        if (result.ItemId != 0)
            //        {
            //            selectedSales = vm.model.TempArticleSalesCache.Where(x => x.Id == result.ItemId).ToList();
            //        }
            //        else if (result.Workaround == Workaround.DuplicatedSales)
            //        {
            //            selectedSales = vm.model.TempArticleSalesCache.Where(x => x.ArticleName == result.Param2 &&
            //                                                                      x.StoreCode == result.Param1 &&
            //                                                                      x.Date.ToString("yyyyMM") == result.Param3).ToList();

            //        }

            //        if (selectedSales != null)
            //        {
            //            vm.model.TempArticleSales = new ObservableCollection<TempArticleSale>(selectedSales);

            //            vm.TriggerPropertyChanged();
            //        }

            //    }



            //    int tempId = result.ItemId;

            //    if (tempId > 0)
            //    {
            //        TempArticleSale sale = vm.TempArticleSales.SingleOrDefault(art => art.Id == tempId);

            //        TempArticleSalesGV.ScrollIntoViewAsync(sale, //the row
            //                             null, //the column
            //                             new Action<FrameworkElement>((f) =>
            //                             {
            //                                 (f as GridViewRow).IsSelected = true; // the callback method; if it is not necessary, you may set that parameter to null;
            //                             }));
            //    }
            //}
        }
    }
}
